package com.ice.crm.activity.controller;

import com.ice.crm.activity.model.Activity;
import com.ice.crm.activity.service.ActivityService;
import com.ice.crm.commons.Page;
import com.ice.crm.user.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
public class ActivityController {
    @Resource
    private ActivityService activityService;

    /**
     * 查询活动列表中的新信息，翻页的实现
     *
     * @param session
     * @param model
     * @param pageNum
     * @param pageSize
     * @param name
     * @param ownerName
     * @param startDate
     * @param endDate
     * @return
     */
    @RequestMapping("/activity/activity_list")
    public String activity(HttpSession session, Model model, Integer pageNum, Integer pageSize, String name, String ownerName, String startDate, String endDate) {
        // 要保证用户有登录状态,获取在浏览器中的用户登录状态，然后获取用户的 id ,然后通过用户的  id 去查询用户信息
//        User user = (User) session.getAttribute("userSession");
//        Integer userId = user.getId();
        Integer userId = 1;
        // 初始化当前页码，如果不初始化没有值，会报错
        pageNum = pageNum == null ? 1 : pageNum;
        // 初始化每页显示的条数
        pageSize = pageSize == null ? 5 : pageSize;

        Page page = activityService.selectActivityLimit(userId, pageNum, pageSize, name, ownerName, startDate, endDate);
        // 将查询回来的信息存储到 model 中，然后在前端进行使用
        model.addAttribute("page", page);
        // 保留请求回来的数据
        model.addAttribute("name", name);
        model.addAttribute("ownerName", ownerName);
        model.addAttribute("startDate", startDate);
        model.addAttribute("endDate", endDate);
        return "workbench/activity/activity_list";
    }

    /**
     * 逻辑删除活动列表中的信息
     *
     * @param activityIds
     * @return
     */
    @RequestMapping("/activity/activityDelete")
    public String activityDelete(Integer activityIds[]) {
        for (Integer activityId : activityIds) {
            System.out.println("activityId:" + activityId);
        }
        activityService.activityDelete(activityIds);
        // 重定向到查询列表的方法
        return "redirect:/activity/activity_list";
    }

    @RequestMapping("/activity/addListActivity")
    public String addListActivity() {
        return "workbench/activity/activity_add";
    }

    @RequestMapping("/activity/activityAdd")
    public String activityAdd(Activity activity) {
        //User user = (User) session.getAttribute("userSession");
        //Integer userId = user.getId();
        Integer userId = 1;
        activity.setCreateBy(userId);
        activity.setCreateTime(new Date());
        activity.setStatus(1);
        activityService.activityAdd(activity);
        return "redirect:/activity/activity_list";
    }
}
